package cn.online.taxi.common.test;

public class Maze {

    private static int startPosI = 1;
    private static int startPosJ = 1;
    private static int endPosI = 8;
    private static int endPosJ = 8;

    public static boolean visited(int[][] maze, int i, int j) {
        maze[i][j] = 2;
        if(i == endPosI && j == endPosJ) {
            for(int m = 0; m < maze.length; m++) {
                for(int n = 0; n <maze[m].length; n++) {
                    if(maze[m][n] == 0) {
                        System.out.print("# ");
                    } else if(maze[m][n] == 2) {
                        System.out.print("* ");
                    } else {
                        System.out.print("1 ");
                    }
                }
                System.out.println();
            }
            System.out.println("结束");
            return false;
        }
        //向下
        if(maze[i+1][j] == 1) {
            visited(maze, i+1, j);
        }
        //向上
        if(maze[i-1][j] == 1) {
            visited(maze, i-1, j);
        }
        // 向左
        if(maze[i][j-1] == 1) {
            visited(maze, i, j-1);
        }
        // 向右
        if(maze[i][j+1] == 1) {
            visited(maze, i, j+1);
        }
        maze[i][j] = 1;
        return true;
    }

    public static void main(String[] args) {
        // 初始化迷宫
        int[][] maze = new int[10][10];
        for(int i = 0; i < maze.length; i++) {
            for(int j = 0; j < maze[i].length; j++) {
                if((i == 0 || i == 9) || (j == 0 || j == 9)) {
                    maze[i][j] = 0;
                }
                else {
                    maze[i][j] = 1;
                }
            }
        }
        // 加工迷宫
        maze[1][3] = 0;
        maze[1][7] = 0;
        maze[2][3] = 0;
        maze[2][7] = 0;
        maze[3][5] = 0;
        maze[3][6] = 0;
        maze[4][2] = 0;
        maze[4][3] = 0;
        maze[4][4] = 0;
        maze[5][4] = 0;
        maze[6][2] = 0;
        maze[6][6] = 0;
        maze[7][2] = 0;
        maze[7][3] = 0;
        maze[7][4] = 0;
        maze[7][6] = 0;
        maze[7][7] = 0;
        maze[8][1] = 0;
        for(int i = 0; i < maze.length; i++) {
            for(int j = 0; j < maze[i].length; j++) {
                System.out.print(maze[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("路线");
        visited(maze, startPosI, startPosJ);
    }

}
